<?php
namespace app\admin\controller;

use app\common\model\Act as ActModel;
use app\common\model\Recharge as RechargeModel;
use app\common\model\Company as CompanyModel;
use app\common\model\ActsOrder as ActsOrderModel;

/**
 * 项目控制器
 * Class Act
 * @package app\index\controller
 */
class Act extends Base
{
    /**
     * [index  项目列表]
     * @param
     * @return mixed|\think\response\Json
     * @author [C。]
     */
    public function getIndex()
    {
        $data = input('param.');
        $model = new ActModel();
        $user = $this->user;
        if(!empty($data['do'])){
            if($data['do'] == 'find'){
                try {
                    $res = $model->findData($data['id']);
                    $http = substr($res['cover'],0,4);
                    if($http != 'http'){
                        $res['cover'] = config('domain_name').$res['cover'];
                    }
                } catch (\Exception $e) {
                    systembug($e->getMessage(),1,'admin/act/getIndex?do=find');
                    return admin_json(-1, '获取失败'.$e->getMessage());
                }
                writelog('查看项目'.$data['id'],1,$user);
                return admin_json(1,'获取成功',$res);
            }
        }

        $rmodel = new RechargeModel();
        $omodel = new ActsOrderModel;
        $map = [];
        if (is_array($data)) {
            if (!empty($data['key'])){
                if($data['key'] == 1){
                    $map['a.title'] = ['like', "%" . $data['con'] . "%"];
                }elseif($data['key'] == 2){
                    $map['c.comname'] = ['like', "%" . $data['con'] . "%"];
                }
            }
            if(!empty($data['start']) && !empty($data['end'])){
                $map['a.addtime'] = ['between',[strtotime($data['start']),strtotime($data['end'])]];
            }
        }
        $map['a.states'] = ['not in' ,'2,-1'];
        $this->getPageAndSize($data);
        try {
            $count = $model->getActCountByCondition($map);//满足条件的总数
            $allpage = intval(ceil($count / $this->size));//总页数
            $lists = $model->getActByConditon($map, $this->from, $this->size);
            foreach($lists as $k=>$v){
                $amt = $rmodel->getAlreadyRaising($v['id']);
                $amt1 = $omodel->getAlreadyRaising($v['id']);
                $lists[$k]['amt'] = $amt + $amt1;
                $http = substr($v['cover'],0,4);
                $lists[$k]['addtime'] = date('Y-m-d H:i:s',$v['addtime']);
                if($http != 'http'){
                    $lists[$k]['cover'] = config('domain_name').$v['cover'];
                }
            }
        } catch (\Exception $e) {
            systembug($e->getMessage(),1,'admin/act/getIndex');
            return admin_json(-1, '获取失败'.$e->getMessage());
        }

        writelog('查看项目列表',0,$user);

        return admin_json(1, '获取成功',[
            'count' => $count,//总记录数
            'allpage' => $allpage,  //总页数
            'lists' => $lists,  //列表数据
        ]);
    }
    /**
     * [getCompany  根据条件获取寺院]
     * @param
     * @return mixed|\think\response\Json
     * @author [C。]
     */
    public function getCompany()
    {
        $data = input('param.');
        $model = new CompanyModel();
        $map = [];
        $user = $this->user;
        if(!empty($data['con'])){
            $map['comname'] = ['like', "%" . $data['con'] . "%"];
        }
        try{
            $list = $model->getCompanyByConditon($map,'comname');
        }catch(\Exception $e){
            systembug($e->getMessage(),1,'admin/act/getCompany');
            return admin_json(-1, '根据条件查找寺院失败'.$e->getMessage());
        }
        writelog('根据条件查找寺院成功',0,$user);
        return admin_json(1, '获取成功',$list);
    }
    /**
     * [index  添加项目]
     * @param
     * @return mixed|\think\response\Json
     * @author [C。]
     */
    public function postAdd()
    {
        $data = input('param.');
        $model = new ActModel();
        $user = $this->user;
        try {
            $data['addtime'] = time();
            $id = $model->addData($data);
        } catch (\Exception $e) {
            systembug($e->getMessage(),1,'admin/act/postAdd');
            return admin_json(-1,'添加项目失败');
        }

        writelog( '项目添加成功--'.$id,0,$user);

        return admin_json(1,'项目添加成功');

    }
    /**
     * [postedit  编辑项目]
     * @param
     * @return mixed|\think\response\Json
     * @author [C。]
     */
    public function postEdit()
    {
        $data = input('param.');
        $model = new ActModel();
        $user = $this->user;
        try {
            $data['edittime'] = time();
            $id = $model->editData($data);
        } catch (\Exception $e) {
            systembug($e->getMessage(),1,'admin/act/postEdit');
            return admin_json(-1,'编辑项目失败');
        }

        writelog( '编辑项目成功--'.$id,0,$user);

        return admin_json(1,'编辑项目成功');

    }
    /**
     * [postedit  删除项目]
     * @param
     * @return mixed|\think\response\Json
     * @author [C。]
     */
    public function getDel()
    {
        $data = input('param.');
        $model = new ActModel();
        $user = $this->user;
        try {
            $model->deleteFalseData(['id'=>$data['id']],['states'=>-1,'edittime'=>time(),'deltime'=>time()]);
        } catch (\Exception $e) {
            systembug($e->getMessage(),1,'admin/act/getDel');
            return admin_json(-1,'删除项目失败');
        }
        writelog( '删除项目成功--'.$data['id'],0,$user);

        return admin_json(1,'删除项目成功');
    }
    /**
     * [getCate 项目回收站]
     * @param
     * @return mixed|\think\response\Json
     * @author [C。]
     */
    public function getRecall(){
        $data = input('param.');
        $model = new ActModel();
        $rmodel = new RechargeModel();
        $map = [];

        $map['a.states'] = -1;
        $this->getPageAndSize($data);
        $user = $this->user;
        try {
            $count = $model->getActCountByCondition($map);//满足条件的总数
            $allpage = intval(ceil($count / $this->size));//总页数
            $lists = $model->getActByConditon($map, $this->from, $this->size);
            foreach($lists as $k=>$v){
                $amt = $rmodel->getAlreadyRaising($v['id']);
                $lists[$k]['amt'] = $amt;
                $http = substr($v['cover'],0,4);
                $lists[$k]['addtime'] = date('Y-m-d H:i:s',$v['addtime']);
                if($http != 'http'){
                    $lists[$k]['cover'] = config('domain_name').$v['cover'];
                }
            }
        } catch (\Exception $e) {
            systembug($e->getMessage(),1,'admin/act/getRecall');
            return admin_json(-1, '获取失败'.$e->getMessage());
        }

        writelog('查看项目回收站',0,$user);
        return admin_json(1, '获取成功',[
            'count' => $count,//总记录数
            'allpage' => $allpage,  //总页数
            'lists' => $lists,  //列表数据
        ]);
    }
    /**
     * [getCate 项目回收站清空恢复]
     * @param $param['states'] 0 1回复 2清空
     * @return mixed|\think\response\Json
     * @author [C。]
     */
    public function getEmpty(){
        $param = input('param.');
        $model = new ActModel;
        $user = $this->user;
        try{
            $model->editData($param);
        }catch(\Exception $e){
            systembug($e->getMessage(),1,'admin/act/getEmpty');
            return admin_json(-1, '失败'.$e->getMessage());
        }
        if($param['states'] == 1){
            $name = '恢复';
        }elseif($param['states'] == 2){
            $name = '清空';
        }
        writelog($name.'乐善好施：id'.$param['id'],0,$user);
        return admin_json(1, '成功');
    }
}